package channelLogger

import (
	"gitee.com/fierce_wolf/go-fox-edge-common/commConfig"
	"gitee.com/fierce_wolf/go-fox-edge-common/commLogger"
	"gitee.com/fierce_wolf/go-fox-edge-common/commUtil/Json"
	"gitee.com/fierce_wolf/go-fox-edge-common/commUtil/Map"
	"gitee.com/fierce_wolf/go-fox-edge-common/commUtil/Reflect"
)

func PrintChannelDebugLogger(channelName, tag string, content interface{}) {
	printChannelLogger("DEBUG", channelName, tag, content)
}

func PrintlDebugLogger(message string) {
	PrintLogger("DEBUG", message)
}

func PrintlErrorLogger(message string) {
	PrintLogger("ERROR", message)
}

func PrintlInfoLogger(message string) {
	PrintLogger("INFO", message)
}

func PrintlWarnLogger(message string) {
	PrintLogger("WARN", message)
}

func printChannelLogger(level, channelName, tag string, content interface{}) {
	configValue, err := commConfig.GetConfigParam(configName)
	if err != nil {
		return
	}

	// 检测：是否需要记录日志
	if !Map.GetBool(configValue, configValueLogger, false) {
		return
	}

	// 检查：是否配置了限定通道范围
	name, ok := configValue["channelName"]
	if ok {
		if channelName != name {
			return
		}
	}

	message := ""
	if content == nil {
		message = "通道名称：" + channelName + "\n" + tag + "：null"
	}
	if Reflect.IsString(content) {
		message = "通道名称：" + channelName + "\n" + tag + "：" + content.(string)
	}
	if Reflect.IsMap(content) || Reflect.IsSlice(content) {
		message = "通道名称：" + channelName + "\n" + tag + "：" + Json.BuildJson(content)
	}

	printLogger(level, message)
}

func PrintLogger(level, message string) {
	if message == "" {
		return
	}

	configValue, err := commConfig.GetConfigParam(configName)
	if err != nil {
		return
	}

	// 检测：是否需要记录日志
	if !Map.GetBool(configValue, configValueLogger, false) {
		return
	}

	printLogger(level, message)
}

func printLogger(level, message string) {
	if "INFO" == level {
		commLogger.Info(message)
		return
	}
	if "ERROR" == level {
		commLogger.Error(message)
		return
	}
	if "DEBUG" == level {
		commLogger.Debug(message)
		return
	}
	if "WARN" == level {
		commLogger.Warn(message)
		return
	}
}
